from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet

from apps.goods.models import SpecificationOption, SPUSpecification
from apps.meiduo_admin.serializers import SpecificationOptionSerializer, SPUSpecsSerializer
from apps.meiduo_admin.utils import CurPagination


class Spec_OptionsView(ModelViewSet):
    permission_classes = [IsAdminUser]

    queryset = SpecificationOption.objects.order_by('spec_id')
    serializer_class = SpecificationOptionSerializer

    pagination_class = CurPagination

    def simplespuspecs(self, request,  *args, **kwargs):
        instances = SPUSpecification.objects.order_by('spu_id')
        ser = SPUSpecsSerializer(instances, many=True)
        data = [{'id': spec['id'], 'name': spec['spu'] + ':' + spec['name']}for spec in ser.data]
        return Response(data)
